import copy

def recognizer(code):
    outList = []
    # state = ('0', '1', '2', '3', '4', '5', '6')  # 状态
    state = 0  # 初始状态为1
    i = 0
    id = ''  # 标识符
    while i<len(code):
        char = code[i]        
        match state:
            case 0:                
                if ('A' <= char <= 'Z') or ('a' <= char <= 'z') or (char == '_'):
                    id += char
                    state = 1
                    i += 1
                elif ('0' <= char <= '9'):
                    id += char
                    state = 2
                    i += 1
                elif char in ('+','-','/','*','>','<','=','{','}','(',')',';'):
                    id += char
                    state = 4
                else:
                    state = 5                
            case 1: # S1：标识符状态
                if ('A' <= char <= 'Z') or ('a' <= char <= 'z') or ('0' <= char <= '9') or (char == '_'):
                    id += char
                    state = 1
                    i += 1
                else:
                    state = 5
            case 2:
                if  ('0' <= char <= '9') :
                    id += char
                    state = 2
                    i += 1
                elif char=='.':
                    id += char
                    state = 3
                    i += 1
                else:
                    state = 5
            case 3: # S3：浮点数状态
                if ('0' <= char <= '9'):
                    id += char
                    state = 3
                    i += 1
                else:
                    state = 5
            case 4:#S4：操作符状态
                if len(id)!=0:
                    outList.append(copy.deepcopy(id))
                    id = ''
                state = 0 # 回到初始状态
                i += 1
            case 5:
                if len(id)!=0:
                    outList.append(copy.deepcopy(id)) #回退一个字符
                    i -= 1
                    id = ''
                state = 0 # 回到初始状态
                i += 1
        
    return outList

if __name__ == '__main__':
    # code = "int var1; float var2;"
    code = "int var1; float var2;var1=5;var2=7.8;float c=var1*var2;"
    print(code)

    ids = recognizer(code)
    print(ids)
